package gui.dialog;

import gui.TuristAssistant;
import gui.tab.ChatCompositeTab;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.example.www.communication.ChatService.ChatRoom;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * Dialogo para chatear dentro de una sala.
 */
public class CommChatRoomDialog extends org.eclipse.swt.widgets.Dialog {
	private ChatRoom			chatRoom;
	private ChatCompositeTab	father;
	private String				entireChat;
	
	private Shell				dialogShell;
	private Button				swtbtnSendMsg;
	private Text				swttxtaMsgs;
	private Text				swttxtWriteMsg;
	
	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			CommChatRoomDialog inst = new CommChatRoomDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Constructor basado en el constructor super.
	 */
	public CommChatRoomDialog(Shell parent, int style) {
		super(parent, style);
	}
	
	/**
	 * Abre y muestra el dialogo. Antes de invocar este metodo se deve setear
	 * los campos internos mediante {@link #setChatRoom(ChatRoom)},
	 * {@link #setFather(ChatCompositeTab)}.
	 */
	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
			/* | SWT.APPLICATION_MODAL */);
			
			dialogShell.setLayout(new FormLayout());
			dialogShell.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent evt) {
					dialogShellWidgetDisposed(evt);
				}
			});
			{
				FormData swttxtWriteMsgLData = new FormData();
				swttxtWriteMsgLData.width = 337;
				swttxtWriteMsgLData.height = 28;
				swttxtWriteMsgLData.left = new FormAttachment(0, 1000, 18);
				swttxtWriteMsgLData.top = new FormAttachment(735, 1000, 0);
				swttxtWriteMsgLData.right = new FormAttachment(790, 1000, 0);
				swttxtWriteMsgLData.bottom = new FormAttachment(952, 1000, 0);
				swttxtWriteMsg = new Text(dialogShell, SWT.MULTI | SWT.V_SCROLL
						| SWT.BORDER);
				swttxtWriteMsg.setLayoutData(swttxtWriteMsgLData);
			}
			{
				swtbtnSendMsg = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				FormData swtbtnSendMsgLData = new FormData();
				swtbtnSendMsgLData.width = 75;
				swtbtnSendMsgLData.height = 30;
				swtbtnSendMsgLData.left = new FormAttachment(819, 1000, 0);
				swtbtnSendMsgLData.top = new FormAttachment(746, 1000, 0);
				swtbtnSendMsgLData.right = new FormAttachment(975, 1000, 0);
				swtbtnSendMsgLData.bottom = new FormAttachment(939, 1000, 0);
				swtbtnSendMsg.setLayoutData(swtbtnSendMsgLData);
				swtbtnSendMsg.setText("Send");
				swtbtnSendMsg.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						swtbtnSendMsgMouseUp(evt);
					}
				});
				swtbtnSendMsg
						.setImage(new Image(Display.getCurrent(),
								"resource/tango-icon-theme-0.8.90/16x16/actions/edit-redo.png"));
			}
			{
				FormData swttxtaMsgsLData = new FormData();
				swttxtaMsgsLData.width = 439;
				swttxtaMsgsLData.height = 105;
				swttxtaMsgsLData.left = new FormAttachment(0, 1000, 14);
				swttxtaMsgsLData.top = new FormAttachment(0, 1000, 15);
				swttxtaMsgsLData.right = new FormAttachment(976, 1000, 0);
				swttxtaMsgsLData.bottom = new FormAttachment(662, 1000, 0);
				swttxtaMsgs = new Text(dialogShell, SWT.MULTI | SWT.READ_ONLY
						| SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
				swttxtaMsgs.setLayoutData(swttxtaMsgsLData);
				swttxtaMsgs.setEditable(false);
				swttxtaMsgs.setOrientation(SWT.HORIZONTAL);
			}
			
			{
				dialogShell.setText("ChatRoom: "
						+ this.chatRoom.getChatRoomID());
				dialogShell
						.setImage(new Image(Display.getCurrent(),
								"resource/tango-icon-theme-0.8.90/16x16/apps/internet-group-chat.png"));
				this.entireChat = "";
				this.addMsg("Now Connected to Chat Room:\r\n"
						+ this.chatRoom.getChatRoomID());
			}
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param chatRoom
	 *        the chatRoom to set
	 */
	public final void setChatRoom(ChatRoom chatRoom) {
		this.chatRoom = chatRoom;
	}
	
	/**
	 * @return the chatRoom
	 */
	public final ChatRoom getChatRoom() {
		return chatRoom;
	}
	
	private void swtbtnSendMsgMouseUp(MouseEvent evt) {
		if (!this.swttxtWriteMsg.getText().equals("")) {
			addMsg(TuristAssistant.chatClient.sendMsg(this.chatRoom
					.getChatRoomID(), this.swttxtWriteMsg.getText()));
			this.swttxtWriteMsg.setText("");
		}
	}
	
	/**
	 * Agrega un mensaje al text area donde se ve la conversacion.
	 */
	public void addMsg(String msg) {
		this.entireChat = this.entireChat + "\r\n" + msg;
		this.swttxtaMsgs.setText(this.entireChat);
		this.swttxtaMsgs.setSelection(entireChat.length(), entireChat.length());
		this.swttxtWriteMsg.setFocus();
	}
	
	/**
	 * @param father
	 *        the father to set
	 */
	public final void setFather(ChatCompositeTab father) {
		this.father = father;
	}
	
	private void dialogShellWidgetDisposed(DisposeEvent evt) {
		father.chatRoomCompDisposed(this);
	}
	
}
